package com.xuanrui.config;


import com.xuanrui.interceptor.LoginInterceptor;
import com.xuanrui.interceptor.OptionsInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


/**
 * @author sunke
 * @Date 2018/8/16 10:32
 * @description
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {


    @Resource(name = "optionsInterceptor")
    private OptionsInterceptor optionsInterceptor;

    @Resource(name = "loginInterceptor")
    private LoginInterceptor loginInterceptor;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/frame/**").addResourceLocations("classpath:/static/frame/");
        registry.addResourceHandler("/style/**").addResourceLocations("classpath:/static/style/");
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/static/fonts/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {


        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS")
                .allowedHeaders("X-Requested-with", "Content-Type", "wechatauthorization", "jsonType", "json-type")
                .exposedHeaders("wechatauthorization", "X-Requested-With", "Content-Type", "jsonType", "json-type")
                .allowCredentials(false).maxAge(3600);

    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(optionsInterceptor).addPathPatterns("/**");

        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                .excludePathPatterns(
                        "/MP_verify_*.txt",
                        "/wechat/signature",
                        "/wechat/silentLogin/*",
                        "/adapay/callback",//支付回掉
                        "/adapay/pay",//支付回掉
                        "/files/**",
                        "/imageCode",
                        "/html2Img",
                        "/webjars/**");


    }


}
